package com.cool.service.sysmanage.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.cool.bo.sysmanage.common.PageQueryBo;
import com.cool.bo.sysmanage.role.AllocResource;
import com.cool.mapper.sysmanagemapper.DdRoleMenuMapper;
import com.cool.mapper.sysmanagemapper.MenuPoMapper;
import com.cool.mapper.sysmanagemapper.RolePermissionPoMapper;
import com.cool.mapper.sysmanagemapper.RolePoMapper;
import com.cool.po.sysmanagepo.MenuPo;
import com.cool.po.sysmanagepo.PermissionPo;
import com.cool.po.sysmanagepo.RolePo;
import com.cool.service.sysmanage.DdRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.List;
import java.util.Objects;

/**
 * 角色表(DdRole)表服务实现类
 *
 * @author Jungle
 * @since 2020-07-27 17:28:34
 */
@Service("ddRoleService")
@Transactional(rollbackFor = Exception.class)
public class DdRoleServiceImpl implements DdRoleService {
    @Resource
    private RolePoMapper rolePoMapper;
    @Resource
    private MenuPoMapper menuPoMapper;
    @Resource
    private DdRoleMenuMapper ddRoleMenuMapper;
    @Resource
    private RolePermissionPoMapper rolePermissionPoMapper;

    @Override
    public List<RolePo> getAllUserRole() {
        return rolePoMapper.getAllUserRole();
    }

    @Override
    public List<RolePo> selectListById(Integer id) {
        return rolePoMapper.selectListById(id);
    }

    @Override
    public Integer updateUserRole(String adminId, String roleIds) {
        //先删除
        if(Objects.isNull(adminId)){
            return -1;
        }
        Integer integer = Integer.valueOf(adminId);
        
        if(Objects.isNull(roleIds)){
            return -1;
        }
        String[] split = roleIds.split(",");

        //先删除
        int i = rolePoMapper.deleteMapConnection(integer);
        //增加
        DateTime date = DateUtil.date();
        Integer len = rolePoMapper.addCollection(integer,split,date);


        return len;
    }

    /**
     * 分页查询
     *
     * @param rolePo
     * @return {@link PageQueryBo }
     * @author Jiangmanman
     * @date 2020/07/29
     */
    @Override
    public PageQueryBo pagingQuery(RolePo rolePo) {

        PageHelper.startPage(rolePo.getPageNum(), rolePo.getPageSize());

        List<RolePo> list = rolePoMapper.selectAll(rolePo);

        PageInfo<RolePo> staffPoPageInfo = new PageInfo<>(list);

        PageQueryBo pageQueryBo = new PageQueryBo(
                staffPoPageInfo.getPageSize(),
                staffPoPageInfo.getPageNum(),
                staffPoPageInfo.getPages(),
                staffPoPageInfo.getTotal(),
                staffPoPageInfo.getList()
        );


        return pageQueryBo;
    }

    /**
     * 插入
     *
     * @param rolePo 角色
     * @return {@link Integer }
     * @author Jiangmanman
     * @date 2020/07/29
     */
    @Override
    public Integer insert(RolePo rolePo) {

        DateTime date = DateUtil.date();

        rolePo.setRoleCreate(date);
        rolePo.setRoleUpdate(date);

        return rolePoMapper.insert(rolePo);
    }

    /**
     * 更新状态
     *
     * @param id    id
     * @param state 状态
     * @return {@link Integer }
     * @author Jiangmanman
     * @date 2020/07/29
     */
    @Override
    public Integer updateStatus(Integer id, Integer state) {


        return rolePoMapper.updateStateById(id,state);
    }

    /**
     * 更新的作用
     *
     * @param id     id
     * @param rolePo 角色
     * @return {@link Integer }
     * @author Jiangmanman
     * @date 2020/07/29
     */
    @Override
    public Integer updateRole(Integer id, RolePo rolePo) {
        return rolePoMapper.updateByPrimaryKeySelective(rolePo);
    }

    @Override
    public Integer deleteById(String ids) {
        Integer integer;
        try {
            integer = Integer.valueOf(ids);
        }catch (NumberFormatException e){
            return -1;
        }
        return rolePoMapper.deleteByPrimaryKey(integer);
    }

    @Override
    public List<PermissionPo> getUserPermissionByRoleId(Integer stuffId) {



        return rolePoMapper.getUserPermissionByRoleId(stuffId);
    }

    @Override
    public Integer allocResource(AllocResource allocResource) {


        Integer lenOne = rolePermissionPoMapper.deleteByRoleId(allocResource.getRoleId());

        DateTime date = DateUtil.date();

        if(Objects.isNull(allocResource.getResourceIds()) || allocResource.getResourceIds().size() <1){

            return 1;
        }
        Integer lenTwo = rolePermissionPoMapper.bulkInsert(allocResource,date);

        return lenOne+lenTwo;
    }

    /**
     * 更新alloc菜单
     *
     * @param roleid  roleid
     * @param menuIds 菜单id
     * @return {@link Integer }
     * @author Jiangmanman
     * @date 2020/07/29
     */
    @Override
    public Integer updateAllocMenu(String roleid, String menuIds) {

        int len = -1;
        try {

            Integer roled = Integer.valueOf(roleid);
            String[] menuId = menuIds.split(",");

            len= ddRoleMenuMapper.deleteByRoleId(roled);

            if(menuId.length >1){
                int insertLen = rolePoMapper.insertStuffMenu(roled,menuId, DateUtil.date());

                if(insertLen < 1){
                    throw new RuntimeException("增加用户拥有权限失败");
                }
            }
        }catch (RuntimeException e){


            return -1;
        }
        return 1;
    }

    /**
     * 选择菜单列表
     *
     * @param id id
     * @return {@link List<MenuPo> }
     * @author Jiangmanman
     * @date 2020/07/29
     */
    @Override
    public List<MenuPo> selectMenuList(Integer id) {
        return rolePoMapper.selectMenuList(id);
    }
}